package cc.mxtt.mvc.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import cc.mxtt.common.core.BaseController;
import cc.mxtt.common.core.GlobalKey;
import cc.mxtt.mvc.interceptor.UserLogInterceptor;
import cc.mxtt.mvc.model.Dept;
import cc.mxtt.mvc.model.Project;
import cc.mxtt.mvc.model.Role;
import cc.mxtt.mvc.model.RolePower;
import cc.mxtt.mvc.model.User;
import cc.mxtt.mvc.model.UserRole;
import cc.mxtt.mvc.service.ProjectService;
import cc.mxtt.mvc.service.impl.ProjectServiceImpl;

import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;


public class ProjectController extends BaseController {
	ProjectService service = new ProjectServiceImpl();
	
	public void index(){
		render("index.jsp");
	}
	
	@Before(UserLogInterceptor.class)
	public void list(){
		
		Project pro = new Project();
		//id=  name=ss  address=ss  jiandu=ss  jiance=ss  jianli person=sss  time=2016-03-11  jianshe=ss  sheji=ss  kancha=ss  jizhuang=ss  weituo=ss
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long deptId = user.getLong("dept_id");
		System.out.println("deptId:"+deptId);
		String sql = "select t.id,t.name,t.description,t.parent_id,t.order_id from sys_dept t where t.id=?";
		Dept dept = Dept.dao.findFirst(sql,deptId);
		Long id = dept.getLong("parent_id");
		System.out.println("parent_id:"+id);
		
		Long userId = user.getLong("id");
		
		UserRole menu = UserRole.dao.findFirst("select * from sys_user_role where user_id =?" , userId);
		Role role = Role.dao.findFirst("select * from sys_role where id =?", menu.getLong("role_id"));
		String roleName = role.getStr("name");
		
		
		int pageNumber = getParaToInt("page",1);
		int pageSize = getParaToInt("rows",10);
		String name = getPara(0,""); //搜索条件，如果没有则为空
		try {
			name = URLDecoder.decode(name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Page<Project> listuser = null;
		
		if(roleName.equals("系统管理员")){
			listuser = service.getDataGrid(pageNumber, pageSize, name);
		}else{
			listuser = service.getDataGrid(pageNumber, pageSize, name , id);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", listuser.getTotalRow());
		result.put("rows", listuser.getList());
		System.out.println("project:"+JsonKit.toJson(result).toString());
		renderJson(JsonKit.toJson(result));
		
	}
	
	@Before(UserLogInterceptor.class)
	public void add(){
		Map<String, Object> result = new HashMap<String, Object>();
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long userId = user.getLong("id");
		boolean is  = RolePower.dao.getRole(userId , 8);
		if(!is){
			result.put("flag", false);
			result.put("Msg", "添加失败，请检查权限");
			renderJson(JsonKit.toJson(result));
			
			return;
		}
		
		Project pro = new Project();
		//id=  name=ss  address=ss  jiandu=ss  jiance=ss  jianli person=sss  time=2016-03-11  jianshe=ss  sheji=ss  kancha=ss  jizhuang=ss  weituo=ss
		Long deptId = user.getLong("dept_id");
		System.out.println("userId:"+deptId);
		String sql = "select t.id,t.name,t.description,t.parent_id,t.order_id from sys_dept t where t.id=?";
		Dept dept = Dept.dao.findFirst(sql,deptId);
		Long id = dept.getLong("parent_id");
		
		pro.set("name", getPara("name"));
		pro.set("address", getPara("address"));
		pro.set("jiandu", getPara("jiandu"));
		pro.set("jiance", getPara("jiance"));
		pro.set("person", getPara("person"));
		pro.set("time", getPara("time"));
		pro.set("jianshe", getPara("jianshe"));
		pro.set("sheji", getPara("sheji"));
		pro.set("kancha", getPara("kancha"));
		pro.set("jizhuang", getPara("jizhuang"));
		pro.set("weituo", getPara("weituo"));
		pro.set("jianli", getPara("jianli"));
		pro.set("parent_id", id);
		
		if(pro.save()){
			result.put("flag", true);
			result.put("Msg", "添加成功");
		}else{
			result.put("flag", false);
			result.put("Msg", "添加失败");
		}
		renderJson(JsonKit.toJson(result));
		
	}
	
	@Before(UserLogInterceptor.class)
	public void edit(){
		Map<String, Object> result = new HashMap<String, Object>();
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long userId = user.getLong("id");
		boolean is  = RolePower.dao.getRole(userId , 9);
		if(!is){
			result.put("flag", false);
			result.put("Msg", "添加失败，请检查权限");
			renderJson(JsonKit.toJson(result));
			return;
		}
		
		//id=3  name=测试322  address=测试  jiandu=测试  jiance=测试  person=测试  time=2016-03-23  jianshe=测试  sheji=测试  kancha=测试  jizhuang=测试  weituo=测试  jianli=测试  
		Project pro = Project.dao.findById(getPara("id"));
		pro.set("name", getPara("name"));
		pro.set("address", getPara("address"));
		pro.set("jiandu", getPara("jiandu"));
		pro.set("jiance", getPara("jiance"));
		pro.set("person", getPara("person"));
		pro.set("time", getPara("time"));
		pro.set("jianshe", getPara("jianshe"));
		pro.set("sheji", getPara("sheji"));
		pro.set("kancha", getPara("kancha"));
		pro.set("jizhuang", getPara("jizhuang"));
		pro.set("weituo", getPara("weituo"));
		pro.set("jianli", getPara("jianli"));
		if(pro.update()){
			result.put("flag", true);
			result.put("Msg", "添加成功");
		}else{
			result.put("flag", false);
			result.put("Msg", "添加失败");
		}
		renderJson(JsonKit.toJson(result));
		
	
	}
	
	@Before(UserLogInterceptor.class)
	public void delete(){
		
		Map<String, Object> result = new HashMap<String, Object>();
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long userId = user.getLong("id");
		boolean is  = RolePower.dao.getRole(userId , 10);
		if(!is){
			result.put("flag", false);
			result.put("Msg", "删除失败，请检查权限");
			renderJson(JsonKit.toJson(result));
			return;
		}
		
		boolean flag = true;
		String Msg = "";
		if(Project.dao.deleteById(getPara("id"))){
			result.put("flag", true);
			flag = true;
			Msg = "删除成功";
			
		}else{
			flag = false;
			Msg = "删除失败";
		}
		
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
		
	}
	
	
	
	

}
